package com.anolesoft.ProductRegistration.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.ProductRegistration.dto.*;
import com.anolesoft.ProductRegistration.entity.*;
import com.anolesoft.ProductRegistration.mapper.*;
import com.anolesoft.ProductRegistration.service.ProductRegIstrationService;
import com.anolesoft.amljmes.test.fenguangyingdu.dto.FabrecordDTO;
import com.anolesoft.amljmes.test.fenguangyingdu.dto.FenguangYingduRecordDTO;
import com.anolesoft.amljmes.test.fenguangyingdu.entity.FabrecordDO;
import com.anolesoft.amljmes.test.fenguangyingdu.entity.FenguangYingduRecordDO;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
@Slf4j
public class ProductRegLstrationServiceImpl implements ProductRegIstrationService {
    @Autowired
    private ProuctRegLstrationQueryMapper prouctRegLstrationQueryMapper;
    @Autowired
    private ProductFabrecordMapper productFabrecordMapper;
    @Autowired
    private ProuctReglistrationRecordMapper ProuctReglistrationRecordMapper;

    @Override
    public PageData<List<ProductRegistrationQueryDTO>> selectListByCondition(ProductRegistrationQueryDTO ProductRegistrationQueryDTO, Integer limit, Integer curr) {
        Page<ProductRegistrationDO> page = new Page<>(curr, limit);
        Integer s;
        if (CommonUtils.stringNull(ProductRegistrationQueryDTO.getTransferString()) != null) {
            s = Integer.parseInt(ProductRegistrationQueryDTO.getTransferString());
        } else {
            s = null;
        }
        List<ProductRegistrationDO> productRegistrationQueryDOS = prouctRegLstrationQueryMapper.selectPageList(ProductRegistrationDO.builder()
                .jx(CommonUtils.stringNull(ProductRegistrationQueryDTO.getJx()))
                .th(CommonUtils.warpStringNull(ProductRegistrationQueryDTO.getTh()))
                .job(CommonUtils.stringNull(ProductRegistrationQueryDTO.getJob()))
                .fabrecordid(CommonUtils.stringNull(ProductRegistrationQueryDTO.getFabrecordid()))
                .transfer(s)
                .itemNo(CommonUtils.stringNull((ProductRegistrationQueryDTO.getItemNo()))) .build(), page);
        List<ProductRegistrationQueryDTO> crmBasePartPriceQueryDTOS = productRegistrationQueryDOS.stream()
                .map(t -> BeanUtil.copyProperties(t, ProductRegistrationQueryDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), crmBasePartPriceQueryDTOS);
    }

    public ProductFabrecordDTO queryFabrecordByCode(String code) {
        ProductFabrecordDO source = this.productFabrecordMapper.selectFabrecordByCode(code);
        if (Objects.isNull(source)) {
            return null;
        }
        return BeanUtil.copyProperties(source, ProductFabrecordDTO.class);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBatch(List<ProductReglstrationRecordDTO> ProductReglstrationRecords) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String updateUser = loginUser.getEmpUid();
        ProductReglstrationRecords.forEach(t ->
                {
                    ProductionReglstrationRecordDO source = BeanUtil.copyProperties(t, ProductionReglstrationRecordDO.class);
                    source.setObjuid(CommonUtils.fastUUID());
                    source.setTheUser(updateUser);
                    source.setFactoryId(loginUser.getFacCode());
                    ProuctReglistrationRecordMapper.insert(source);
                }
        );
    }

    @Override
    public String IsState(String zhizaojilutiaoma) {
        return productFabrecordMapper.selectState(zhizaojilutiaoma);
    }

    @Override
    public void revocation(String objuid) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String login_user = loginUser.getEmpUid();
        productFabrecordMapper.revocation(objuid,login_user);
    }
}
